{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests \n",
    "import bs4\n",
    "import re\n",
    "import pandas as pd\n",
    "import urllib.request\n",
    "import json\n",
    "import requests\n",
    "import re\n",
    "import os\n",
    "import random\n",
    "import urllib\n",
    "\n",
    "import MySQLdb\n",
    "db = MySQLdb.connect(host = 'localhost',#本地数据库\n",
    "                             user = 'root', #用户名\n",
    "                             passwd = 'yourpw', #数据库密码\n",
    "                             db = 'mydict', #数据库名\n",
    "                             charset = 'utf8') \n",
    "\n",
    "try:\n",
    "    with open('data.json', 'r') as f:\n",
    "        word_base = json.load(f)\n",
    "except:\n",
    "    word_base = dict()\n",
    "NN = 1\n",
    "\n",
    "def look_up(word):\n",
    "    url = 'https://cn.bing.com/dict/search?q='+word\n",
    "\n",
    "    da = requests.request(url=url,method='get')\n",
    "    soup = bs4.BeautifulSoup(da.text)\n",
    "    rubbish = [s.extract() for s in soup('script')]\n",
    "    soup = soup.body\n",
    "    \n",
    "    try:\n",
    "        qdef = soup.findAll('ul')[1]\n",
    "        quick_def = ''\n",
    "        for s in qdef.findAll('li'):\n",
    "            quick_def = quick_def + s.text + '\\n'\n",
    "        quick_def = quick_def.replace('网络','Web.')\n",
    "    except:\n",
    "        return None\n",
    "    \n",
    "    try:\n",
    "        p = soup.findAll('div', {\"class\": \"hd_prUS\"})[0].text\n",
    "        p = re.findall(r'\\[(.*?)\\]', p)[0]\n",
    "    except:\n",
    "        p = ''\n",
    "    \n",
    "    try:\n",
    "        s_url = soup.find('div', {\"class\": \"hd_tf\"}).a['onclick']\n",
    "        s_url = s_url.replace(\"javascript:BilingualDict.Click(this,'\",'')\n",
    "        s_url = s_url.replace(\"','akicon.png',false,'dictionaryvoiceid')\",'')\n",
    "    except:\n",
    "        s_url = ''\n",
    "    \n",
    "    try:\n",
    "        more_def = ''\n",
    "        for pos in soup.findAll('div', {\"class\": \"li_pos\"}):\n",
    "            w_pos = pos.find('div', {\"class\": \"pos\"}).text\n",
    "            for w_def in pos.findAll('div', {\"class\": \"de_co\"}):\n",
    "                more_def = more_def +w_pos+w_def.text+ '\\n'\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "    return {'word':word,'sound':p,'chinese':quick_def,'more_def':more_def,'sound_url':s_url}\n",
    "\n",
    "def get_definition(w):\n",
    "    global NN\n",
    "    if(w in word_base):\n",
    "        da = word_base[w]\n",
    "    else:   \n",
    "        da = look_up(w)\n",
    "        NN = NN+1\n",
    "        if da != None:\n",
    "            word_base[w] = da\n",
    "        else:\n",
    "            return None\n",
    "    return da\n",
    "\n",
    "def insert_res(w):\n",
    "    sql = 'insert ignore into mydict.dicts values(\"'+w[\"word\"]+'\",\"'+w[\"sound\"]+'\",\"'+w['more_def']+'\",\"'+w[\"chinese\"]+'\")'\n",
    "    cursor = db.cursor()\n",
    "    cursor.execute(sql)\n",
    "    db.commit()   \n",
    "\n",
    "    if os.path.exists('audio/'+w['word']+'.mp3'):\n",
    "        return\n",
    "    else:\n",
    "        try:\n",
    "            url = w['sound_url']\n",
    "            urllib.request.urlretrieve(url,'audio/'+w['word']+'.mp3')\n",
    "        except:\n",
    "            return "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def ListToTable(a):\n",
    "    a = a.replace('*','')\n",
    "    words = []\n",
    "    for aa in a.split(' '):\n",
    "        if aa not in {'','\\n'}:\n",
    "            words.append(aa)\n",
    "\n",
    "    \n",
    "\n",
    "    \n",
    "    hhd = 1     \n",
    "    for word in words:\n",
    "        w = get_definition(word)\n",
    "        if NN % 10 == 0:\n",
    "            with open('data.json', 'w') as f:\n",
    "                json.dump(word_base, f)\n",
    "        if w != None:\n",
    "            try:\n",
    "                insert_res(w)\n",
    "            except:\n",
    "                pass\n",
    "            print('已完成第'+str(hhd)+'个:  '+w['word'])\n",
    "            hhd = hhd+1  \n",
    "\n",
    "    with open('data.json', 'w') as f:\n",
    "        json.dump(word_base, f)\n",
    "\n",
    "    print('ALL DONE!!!\\n')\n",
    "    res = '|||\\n|---|---|\\n'\n",
    "    for w in words:\n",
    "        ch = word_base[w]['chinese'].strip().replace('\\n','<br>')\n",
    "        res = res+ '|**' + w + '**|' + ch + '|\\n'\n",
    " \n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_f = open('26-30.md','r',encoding='utf8').readlines()\n",
    "word_s = []\n",
    "sign = 0\n",
    "for s in my_f:\n",
    "    if '## Section' in s:\n",
    "        sign = 1\n",
    "    elif s.strip() != '' and sign == 1:\n",
    "        sign = 0\n",
    "        word_s.append(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "hh = [ListToTable(s) for s in word_s]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "fin = open('26-30.md','r',encoding='utf8').read()\n",
    "for i in range(len(hh)):\n",
    "    fin = fin.replace(word_s[i],hh[i])\n",
    "f = open('26-30.md','w',encoding='utf8')\n",
    "f.write(fin)\n",
    "f.close()\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"## Section 26 median\\n\\n* unidimensional transcendent aver avow pugnacious deferential embargoe treatise  spate counterforce stimulant concomitant futility sloth elusive paraphernalia gleaming circumstellar churn disk\\n \\n\\n\\n1. Some novelists immodestly idealized and exaggerated the significance of their work, but others, **declining** to exalt the role of the writer, question a **transcendent** view of the art. \\n   * decline有拒绝的意思 to refuse\\n   * 一些小说家会毫不客气的理想化及夸张他们工作的重要意义，但其他小说家拒绝拔高作者的地位，觉得艺术创作也没什么了不起的。\\n   * question在这里相当于decline的意思，但transcendent不认识也就无所谓了，其他小说家question 那种认为 art 是transcendent的view\\n   * transcenden 的意思是 greater, better, more important, or going past or above all others 超常的；杰出的，卓越的；极其伟大的\\n4. The trade in scientific literature in nineteenth-century Germany was so robust that publisher constantly worried about **supplies**  of new titles, an anxiety that gave even relatively undistinguished authors, who made their living writing technical treatises, **strong bargaining positions**. \\n   * market robust 这里可以认为是火爆，供不应求\\n   * 市场火爆说明需求量大，所以出版社会担心供不应求，所以选supply。这种情况下supplier（也就是作者们）会有更大的bargaining power，即使是一帮菜鸡们\\n6. The slow pace of job creation was without precedent for the period of recovery from a recession, but the conditions that conspired to cause the recession were also **atypical**. The stock market declined sharply, and rampant business investment slumped. Then an ensuing  of scandals **weakened**  public trust in the way companies were run. And yet, despite these powerful **counterforces**  to growth, the recession proved surprisingly mild. \\n   * despite those powerful 暗示这些力量应该与growth反向\\n   * 题目逻辑是虽然增长的方向力量强大，但是衰退还是令人惊讶地缓和\\n7. A cure for the common cold has been so elusive that it has become a modern symbol of **futility/pointlessness**. \\n   * 普通感冒治疗的效果如此难以捉摸，以至于它已经成为象征无效的当代符号。\\n   * 感冒治疗、感冒药的效果 elusive，意思是，你不知道药或疗法对引起感冒的病原有啥直接的作用，也许没用，也许看不出有用。所以它象征着没意义。？？？\\n   * ETS狗屁\\n\\n\\n9. If giant X-ray flares churn circumstellar disks enough to keep newborn planets, such as Earth once was, from spiraling into their suns, it would be an ironic twist on our conception of X-ray flares as **dangerous/perilous**. \\n   * 如果巨大的 X 射线耀斑足够强力地搅动恒星周围区域，以至于防止新生行星 —— 比如曾经的地球 —— 沿螺旋轨道跌入它们的太阳，那么，我们以前认为 X 射线耀斑危险，实在是一种充满讽刺意味的错怪。\\n   * disk 盘状物。我个人理解，指的是恒星周围，行星轨道空间所包括的盘状区域。\\n   * perilous是危险的意思\\n   * ironic twist暗示前后取反，实际上是保护，我们还认为是危险，这是一种讽刺的扭曲（曲解）\\n\\n## Section 27 median\\n* flout taxonomist forgo parity stratification countermand stratification obfuscation disparity spurn slippery\\n\\n1. Though many avant-garde writers **flout**  traditional distinctions among literary categories, combining elements of biography and fiction, prose and poetry, this fusion of forms has been slow to catch on with publishers. \\n     * 尽管很多前卫作家无视不同文类之间的传统区别，但杂糅了传记和小说，散文和诗歌的元素，这种风格的融合出版商一直接受不了。\\n     * catch on(with somebody) (受某人)欢迎，(在某人群中)变得流行\\n     * flout 是无视，公然蔑视的意思\\n     * 整体意思是装逼的熊写手不按套路出牌，然后让出版商受不了\\n6. Traditional Vietnamese culture has long promoted the idea of gender equality. Founding myths **celebrate**  the equal division of labor in child care for mothers and fathers. As is often the case, however, theoretical commitments are **incommensurate with** actual processes. In reality, gender-based **stratification** persists.\\n   * 越南的传统文化一直提倡性别平等的理念。其创世神话赞颂父母照顾孩子时的平等分工。然而很常见地，只存在于理论上的许诺与实际情况不相称。现实中两性始终不平等\\n   * Founding myths 和promoted the idea暗示平等只是理念上的，事实上还是不平等\\n   * persists 是动词。。加了第三人称之后就看不出来了。。。就是persist\\n\\n9. There is frequently a protracted time interval between the introduction of an innovative musical composition and its public acceptance; the concert-going public often spurns the **novel/original** in favor of the familiar for a prolonged period. \\n   * 音乐创作方面的新尝试，从面世到被公众接受，通常需要一定时间：音乐会的听众往往更欣赏熟悉的曲目，在相当长的一段时间内拒斥创新。\\n   * 主要是不认识spurn：傲慢或轻蔑地拒绝，唾弃。。。\\n10. The concept of the Hellenistic period in ancient history has proved useful but also **slippery/elusive**, with scholars disagreeing on the dates when the period began and ended. \\n       * 古代史中 希腊化时期 的概念已经被证实可用，但也有一些学者认为它难以定义，无法在具体的起止时间上达成共识。\\n\\n\\n## Section 28 median \\n* proliferation  pervasive scratch winnow stipulate provacative cliquish stymie  vilify rant exculpation diatribe\\n\\n1. In the solar system, collisions involving cosmic object are among the most **pervasive**  processes shaping surfaces: images of many solar objects show a proliferation of impact craters formed throughout the past 4.5 billion years. \\n     * 在太阳系中，天体之间相撞是最普遍的一种塑造其表面形态的程序之一：许多太阳系天体的图像显示，过去的 45 亿年中，撞击造成的凹坑在不断增加\\n     * proliferation 暗示了pervasive？？\\n2. If the study proves that bears are still endemic to the area, the proposal to introduce additional bears of the same species will probably face less opposition, since the plan would then involve **augmenting**  a historic population, not trying to build population from scratch. \\n   * 如果研究结果证明，熊仍然在该地区活动，引入更多同种熊的提案也许不会面临如此多的反对，因为那种情况下提案只是增加了熊的历史规模，而不是引入新物种。\\n   * from scratch 从零开始的，不借助任何现成材料的\\n   * winnow 抛高谷物等借助风力吹去皮壳、树叶等，扬场，筛选，去除(杂质等)\\n3. Folmer's book on Edith Wharton seems far removed from recent trends in literary criticism; this need not to be a fault, except that, in its title and introduction, the book **purports**  to be conversant with contemporary discourse in the field, but in its actual analysis of Wharton's work, it is marked by a very **old-fashioned** approach. \\n   * F 关于 E W 的论著，看起来与近期文学批评的趋势大相径庭；这当然不必然有问题，只是在标题和引言中，作者声称熟知该领域当前的讨论，但其对 W 作品的实际分析，显然采用了过时的方法。\\n   * conversant不是传统的，而是熟知。。。\\n4. The book is not comprehensive but is, instead, **provocative** in the most positive sense: **suggests** rather than settles.\\n   * 这本书不好懂，取而代之从最积极的意义上说，却启发思考：使人浮想联翩而非给出确定的结论\\n   * stipulate (通过合同、约定等方式)事先讲明、规定(某事物)， 不是刺激。。。艹\\n   * provacative 煽动性的，挑衅的，引起争论的，刺激的，引发某种结果的，(性)挑逗的\\n6. In adolescence, **peer** interactions are crucial in forging a self-identity. To be sure, this process often plays out in **cliquish social behavior** as a means of defining and shoring up the sense of self. Kids will seek out like-minded companions, and spurn others who seem different. But when kept within reasonable bounds, this in-group  **clustering** generally evolves into a more mature friendship pattern. \\n7. Few ideas are more **abused/misused** than the notion that cultures evolve in Darwin fashion; many academics have begun writing about cultural evolution, but few treat the underlying Darwinian logic with the care it deserves. \\n   * 有一种观念认为，文化以达尔文论述的方式进化，没有什么理论比这种观念受到更多的误用;许多学者已经开始讨论文化的进化，但没几个学者足够认真地思考达尔文主义最根本的逻辑。\\n   * Few ideas are more **abused/misused** than the notion that cultures evolve in Darwin fashion 没有观点比cultures evolve in Darwin fashion 这个notion更加被误用\\n8. After many years of feeling **stymied/hindered**  by his seniors managers, Clark was becoming hopeful of advancement. \\n   * 狗屎啊啊啊啊啊\\n   * 时间表示前后取反\\n10. Readers looking for another condemnation of private equity firms should look elsewhere, this book is not **a rant against/a diatribe on** such firms. \\n    * 单词不认识\\n    * rant，diatribe表示责骂，抨击\\n\\n\\n## Section 29 median\\n\\n* irascibility insouciance  abstemious surreptitious grandly adjudicate repetitious  premiership vulnerable cast-iron stimulating\\n \\n\\n1. One sometimes hears that Marco Polo introduced pasta to the Western world, having encountered it in China. This durable myth, which (i)**requires**  that nothing should have been known of pasta in Italy until 1295, when Marco Polo returned from the Far East, can easily be **debunked**  by pointing out that there are Italian references to pasta **predate that event**. \\n10. Space is often referred to as the final frontier, as the only realm of which humankind has still to gain substantial understanding, yet the ocean is also another vast area about which our knowledge is **rudimentary/sketchy**.\\n       * sketchy,粗略的；概略的；不完备的\\n       * rudimentary/sketchy表示海洋和太空一样，我们没有太多了解\\n\\n\\n## Section 30 median\\n* irascible paleontologist afresh conflate divulge downright compunction grimy slid tepid lugubrious/gloomy\\n  \\n1. The stories in Yiyun Li's recent collection are distinctive particularly for the strong contrast between their emotional intensity and their consistently **measured**  tone. \\n    * measured 缓慢谨慎的；慎重的；克制的, 不是做作的。。\\n4. Making loans and fighting poverty are normally two of the least glamorous pursuits around, but remarkably enough put the two together, and you have an economic innovation that has become not just **popular** but downright **chic**. \\n   * 根据 not just...but (also)得知两个空格是递进关系\\n   * 借贷和消除贫困是两个最不光彩的两件事情，但是将这两个东西结合起 来就会有一个既受欢迎又特时髦的经济创新。\\n6. Biologists have little **compunction about** drawing the link between the success of humanity and human **sociability**. Indeed, many biologists claim that this attribute, the ability to **cooperate**, or, to put it more sharply, to make individuals subordinate their self-interest to the needs of the group, lies at the root of human achievement. \\n   * 把人类成功与人的社会性联系起来的生物学家从不后悔。实际上，很多生物学家认为，这种能够合作的特点，或者说的更清楚点，能把集体需要置于个人利益之上，正是成就人类的基础\\n   * compunction 内疚，后悔\\n8. Tompkinson's prior donations to the university, while very generous, failed to **portend/predict** the magnitude of her latest gift.\\n   * T 之前对大学的捐赠，尽管非常慷慨，但无法预示她最近这次捐赠的手笔之大\\n   * portend （预示）不是 pretend\\n10. Mortoris is dour and lugubrious/gloomy, seemingly incapable of smiling, let alone laughing. \\n     * lugubrious/gloomy 黑暗的；阴暗的；幽暗的；忧郁的，阴郁的；悲伤的\\n     * 单词混乱，lugubrious不是tepid的意思\\n\\n\""
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}